A BEGINNER'S GUIDE TO LINUX KERNEL DEVELOPMENT (LFD103)
code: zsh
$ uname -m
x86_64
code: zsh
$ sudo apt-get install build-essential vim git cscope libncurses-dev libssl-dev bison flex
code: zsh
cd /linux_work
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git linux_mainline
code: zsh
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git linux_stable
$ git branch -a | grep linux-5
remotes/origin/linux-5.0.y
remotes/origin/linux-5.1.y
remotes/origin/linux-5.10.y
remotes/origin/linux-5.11.y
remotes/origin/linux-5.12.y
remotes/origin/linux-5.13.y
remotes/origin/linux-5.14.y
remotes/origin/linux-5.15.y
remotes/origin/linux-5.16.y
remotes/origin/linux-5.17.y
remotes/origin/linux-5.18.y
remotes/origin/linux-5.2.y
remotes/origin/linux-5.3.y
remotes/origin/linux-5.4.y
remotes/origin/linux-5.5.y
remotes/origin/linux-5.6.y
remotes/origin/linux-5.7.y
remotes/origin/linux-5.8.y
remotes/origin/linux-5.9.y
$ git checkout linux-5.18.y
Updating files: 100% (12740/12740), done.
Branch 'linux-5.18.y' set up to track remote branch 'linux-5.18.y' from 'origin'.
Switched to a new branch 'linux-5.18.y'
$ ls /boot
System.map-5.4.0-121-generic config-5.4.0-122-generic initrd.img-5.4.0-121-generic memtest86+.bin vmlinuz vmlinuz.old
System.map-5.4.0-122-generic grub initrd.img-5.4.0-122-generic memtest86+.elf vmlinuz-5.4.0-121-generic
config-5.4.0-121-generic initrd.img initrd.img.old memtest86+_multiboot.bin vmlinuz-5.4.0-122-generic
$ cp /boot/config-5.4.0-122-generic .config
$ lsmod > /tmp/my-lsmod
$ make LSMOD=/tmp/my-lsmod localmodconfig
#
# configuration written to .config
#
$ make -j3 all
libbpf.c:47:10: fatal error: libelf.h: そのようなファイルやディレクトリはありません
fatal error: gelf.h: そのようなファイルやディレクトリはありません
$ sudo apt-get install libelf-dev
make1: *** 'certs/x509_revocation_list' に必要なターゲット 'debian/canonical-revoked-certs.pem' を make するルールがありません. 中止. $ make drivers/media/test-drivers/vimc/vimc-core.o
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
DESCEND bpf/resolve_btfids
make4: *** ターゲット 'drivers/media/test-drivers/vimc/vimc-core.o' を make するルールがありません. 中止. $ cat /etc/issue
Ubuntu 20.04.4 LTS \n \l
$ uname -r
5.4.0-122-generic
Emulate
x86_64
8192MB
4 CPU Cores
128GB
大体2時間でReboot Now
code: zsh
$ ssh wafuwafu13@192.168.xx.x
wafuwafu13@ubuntu:~$ uname -r
5.15.0-43-generic
wafuwafu13@ubuntu:~$ uname -m
x86_64
code: zsh
wafuwafu13@ubuntu:~$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git linux_stable
Cloning into 'linux_stable'...
remote: Enumerating objects: 89, done.
remote: Counting objects: 100% (89/89), done.
remote: Compressing objects: 100% (40/40), done.
client_loop: send disconnect: Broken pipe, 368.49 MiB | 748.00 KiB/s
code: zsh
$ sudo ssh wafuwafu13@192.168
$ sudo ssh-keygen -R 192.168.
https://scrapbox.io/files/62ea08eeb5aafd001dd756ae.png
code: zsh
$ sudo make modules_install
INSTALL arch/x86/crypto/crc32-pclmul.ko
cp: 'arch/x86/crypto/crc32-pclmul.ko' を stat できません: そのようなファイルやディレクトリはありません
https://scrapbox.io/files/62ea1cb354f612001d7e8a05.png
https://scrapbox.io/files/62ea20389899a500232137b9.png
https://scrapbox.io/files/62ea2311115ab9001d37dbb9.png
https://scrapbox.io/files/62ea22de8072a7001d0ad23e.png
https://scrapbox.io/files/62eb1331d6154b001dce5644.png
https://scrapbox.io/files/62eb136db4dfe9001d486d0b.png
egrep '^\s*(menuentry|submenu)\s' /boot/grub/grub.cfg
https://scrapbox.io/files/62eb139d7757d5002011ec99.png
https://scrapbox.io/files/62eb213e1e38290021cb0f51.png
grub-editenv list
https://scrapbox.io/files/62eb237dbf77ea001d51c08c.png
https://scrapbox.io/files/62eb2569301a90001ddac95f.png
5.4.208選んでも同じ
5.15.0-43generic選択
sudo update-initramfs -u -k 5.4.208
https://scrapbox.io/files/62eb26e21fd083001dbf6139.png
sudo update-grub
Advancedで自動選択されるも not syncing
sudo fdisk -l
https://scrapbox.io/files/62eb2afa189d64001d1b66bf.png
https://scrapbox.io/files/62eb2cd9d7a9400022a8039e.png
https://scrapbox.io/files/62eb41ac38b08c0021a2a03c.png
https://scrapbox.io/files/62eb48b5d6ca1e001d3f62b3.png
code: zsh
$ sudo make bzImage
arch/x86/entry/vdso/vclock_gettime.c: In function ‘__vdso_time’:
arch/x86/entry/vdso/vclock_gettime.c:231:1: error: ‘-mindirect-branch’ and ‘-fcf-protection’ are not compatible
code: zsh
$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
code: zsh
$ sudo apt install build-essential
$ sudo apt -y install gcc-8 g++-8
code: zsh
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8
update-alternatives: 警告: リンクグループ gcc が壊れているため、alternative /usr/bin/gcc-9 の再インストールを強制しています
$ sudo update-alternatives --config gcc
alternative gcc (/usr/bin/gcc を提供) には 2 個の選択肢があります。
選択肢 パス 優先度 状態
------------------------------------------------------------
* 0 /usr/bin/gcc-9 60 自動モード
1 /usr/bin/gcc-8 8 手動モード
2 /usr/bin/gcc-9 60 手動モード
現在の選択 * を保持するには <Enter>、さもなければ選択肢の番号のキーを押してください: 1 update-alternatives: /usr/bin/gcc (gcc) を提供するためにマニュアルモードで /usr/bin/gcc-8 を使います
$ sudo update-alternatives --config g++
$ gcc --version
gcc (Ubuntu 8.4.0-3ubuntu2) 8.4.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ --version
bash: /usr/bin/g++: そのようなファイルやディレクトリはありません
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8
update-alternatives: エラー: alternative g++ はマスターになれません: これは gcc のスレーブです
code: zsh
BUILD arch/x86/boot/bzImage
Setup is 17276 bytes (padded to 17408 bytes).
System is 10557 kB
CRC f99507b6
Kernel: arch/x86/boot/bzImage is ready (#1)
code: zsh
$ sudo make modules
CC sound/soundcore.mod.o
$ sudo make modules_install
$ sudo make install
sh ./arch/x86/boot/install.sh 5.0.21+ arch/x86/boot/bzImage \
System.map "/boot"
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 5.0.21+ /boot/vmlinuz-5.0.21+
update-initramfs: Generating /boot/initrd.img-5.0.21+
run-parts: executing /etc/kernel/postinst.d/pm-utils 5.0.21+ /boot/vmlinuz-5.0.21+
run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 5.0.21+ /boot/vmlinuz-5.0.21+
run-parts: executing /etc/kernel/postinst.d/update-notifier 5.0.21+ /boot/vmlinuz-5.0.21+
run-parts: executing /etc/kernel/postinst.d/xx-update-initrd-links 5.0.21+ /boot/vmlinuz-5.0.21+
I: /boot/initrd.img.old is now a symlink to initrd.img-5.4.0-122-generic
I: /boot/initrd.img is now a symlink to initrd.img-5.0.21+
run-parts: executing /etc/kernel/postinst.d/zz-update-grub 5.0.21+ /boot/vmlinuz-5.0.21+
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Linux イメージを見つけました: /boot/vmlinuz-5.4.0-122-generic
Found initrd image: /boot/initr.img-5.4.0-122-generic
Linux イメージを見つけました: /boot/vmlinuz-5.4.0-121-generic
Found initrd image: /boot/initrd.img-5.4.0-121-generic
Linux イメージを見つけました: /boot/vmlinuz-5.0.21+
Found initrd image: /boot/initrd.img-5.0.21+
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
完了
code: zsh
$ cat Makefile
KDIR := /lib/modules/5.0.21-dev/build
PWD := /home/user/hello_module
obj-m := hello.o
.PHONY: all clean install
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
install:
make -C $(KDIR) M=$(PWD) modules_install
code: zsh
$ sudo make
make -C /lib/modules/5.0.21-dev/build M=/home/user/hello_module modules
make1: ディレクトリ '/home/user/linux_stable' に入ります CC M /home/user/hello_module/hello.o Building modules, stage 2.
MODPOST 1 modules
CC /home/user/hello_module/hello.mod.o
LD M /home/user/hello_module/hello.ko make1: ディレクトリ '/home/user/linux_stable' から出ます $ sudo make install
make -C /lib/modules/5.0.21-dev/build M=/home/user/hello_module modules_install
make1: ディレクトリ '/home/user/linux_stable' に入ります INSTALL /home/user/hello_module/hello.ko
DEPMOD 5.0.21+
make1: ディレクトリ '/home/user/linux_stable' から出ます code: zsh
$ ls
Makefile Module.symvers hello.c hello.ko hello.mod.c hello.mod.o hello.o modules.order
$ ls /lib/modules/5.0.21-dev/
build kernel modules.builtin modules.order source
$ sudo insmod hello.ko
insmod: ERROR: could not insert module hello.ko: Invalid module format
$ sudo make clean
$ sudo vi Makefile
KDIR := /lib/modules/5.4.0-121-generic/build
$ sudo make install
make -C /lib/modules/5.4.0-121-generic/build M=/home/user/hello_module modules_install
make1: ディレクトリ '/usr/src/linux-headers-5.4.0-121-generic' に入ります INSTALL /home/user/hello_module/hello.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:69
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:76
sign-file: certs/signing_key.pem: No such file or directory
DEPMOD 5.4.0-121-generic
Warning: modules_install: missing 'System.map' file. Skipping depmod.
make1: ディレクトリ '/usr/src/linux-headers-5.4.0-121-generic' から出ます $ ls /lib/modules/5.4.0-121-generic/extra
hello.ko
$ sudo insmod /lib/modules/5.4.0-121-generic/extra/hello.ko
insmod: ERROR: could not insert module /lib/modules/5.4.0-121-generic/extra/hello.ko: Invalid module format
カーネルのバージョンとコンパイルしたドライバのバージョンが異なるのが原因
code: zsh
$ sudo vi Makefile
KDIR := /lib/modules/5.4.0-122-generic/build
$ sudo insmod /lib/modules/uname -r/extra/hello.ko
$ sudo rmmod hello
$ sudo dmesg
8083.987781 hello: loading out-of-tree module taints kernel. 8083.987867 hello: module verification failed: signature and/or required key missing - tainting kernel code: zsh
$ sudo grub-reboot '1>4'
$ sudo update-grub
code: zsh
$ uname -r
5.0.21+